home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 127 / PC Guia 127.iso / Software / Produtividade / OpenOffice.org 2.0.1 / openofficeorg4.cab / test_getopt.py < prev    next >
Text File  |  2005-11-19  |  6KB  |  171 lines

  1. # test_getopt.py
  2. # David Goodger <dgoodger@bigfoot.com> 2000-08-19
  3.  
  4. import getopt
  5. from getopt import GetoptError
  6. from test.test_support import verify, verbose, run_doctest
  7. import os
  8.  
  9. def expectException(teststr, expected, failure=AssertionError):
  10.     """Executes a statement passed in teststr, and raises an exception
  11.        (failure) if the expected exception is *not* raised."""
  12.     try:
  13.         exec teststr
  14.     except expected:
  15.         pass
  16.     else:
  17.         raise failure
  18.  
  19. if verbose:
  20.     print 'Running tests on getopt.short_has_arg'
  21. verify(getopt.short_has_arg('a', 'a:'))
  22. verify(not getopt.short_has_arg('a', 'a'))
  23. expectException("tmp = getopt.short_has_arg('a', 'b')", GetoptError)
  24. expectException("tmp = getopt.short_has_arg('a', '')", GetoptError)
  25.  
  26. if verbose:
  27.     print 'Running tests on getopt.long_has_args'
  28. has_arg, option = getopt.long_has_args('abc', ['abc='])
  29. verify(has_arg)
  30. verify(option == 'abc')
  31. has_arg, option = getopt.long_has_args('abc', ['abc'])
  32. verify(not has_arg)
  33. verify(option == 'abc')
  34. has_arg, option = getopt.long_has_args('abc', ['abcd'])
  35. verify(not has_arg)
  36. verify(option == 'abcd')
  37. expectException("has_arg, option = getopt.long_has_args('abc', ['def'])",
  38.                 GetoptError)
  39. expectException("has_arg, option = getopt.long_has_args('abc', [])",
  40.                 GetoptError)
  41. expectException("has_arg, option = " + \
  42.                      "getopt.long_has_args('abc', ['abcd','abcde'])",
  43.                 GetoptError)
  44.  
  45. if verbose:
  46.     print 'Running tests on getopt.do_shorts'
  47. opts, args = getopt.do_shorts([], 'a', 'a', [])
  48. verify(opts == [('-a', '')])
  49. verify(args == [])
  50. opts, args = getopt.do_shorts([], 'a1', 'a:', [])
  51. verify(opts == [('-a', '1')])
  52. verify(args == [])
  53. #opts, args = getopt.do_shorts([], 'a=1', 'a:', [])
  54. #verify(opts == [('-a', '1')])
  55. #verify(args == [])
  56. opts, args = getopt.do_shorts([], 'a', 'a:', ['1'])
  57. verify(opts == [('-a', '1')])
  58. verify(args == [])
  59. opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2'])
  60. verify(opts == [('-a', '1')])
  61. verify(args == ['2'])
  62. expectException("opts, args = getopt.do_shorts([], 'a1', 'a', [])",
  63.                 GetoptError)
  64. expectException("opts, args = getopt.do_shorts([], 'a', 'a:', [])",
  65.                 GetoptError)
  66.  
  67. if verbose:
  68.     print 'Running tests on getopt.do_longs'
  69. opts, args = getopt.do_longs([], 'abc', ['abc'], [])
  70. verify(opts == [('--abc', '')])
  71. verify(args == [])
  72. opts, args = getopt.do_longs([], 'abc=1', ['abc='], [])
  73. verify(opts == [('--abc', '1')])
  74. verify(args == [])
  75. opts, args = getopt.do_longs([], 'abc=1', ['abcd='], [])
  76. verify(opts == [('--abcd', '1')])
  77. verify(args == [])
  78. opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], [])
  79. verify(opts == [('--abc', '')])
  80. verify(args == [])
  81. # Much like the preceding, except with a non-alpha character ("-") in
  82. # option name that precedes "="; failed in
  83. # http://sourceforge.net/bugs/?func=detailbug&bug_id=126863&group_id=5470
  84. opts, args = getopt.do_longs([], 'foo=42', ['foo-bar', 'foo=',], [])
  85. verify(opts == [('--foo', '42')])
  86. verify(args == [])
  87. expectException("opts, args = getopt.do_longs([], 'abc=1', ['abc'], [])",
  88.                 GetoptError)
  89. expectException("opts, args = getopt.do_longs([], 'abc', ['abc='], [])",
  90.                 GetoptError)
  91.  
  92. # note: the empty string between '-a' and '--beta' is significant:
  93. # it simulates an empty string option argument ('-a ""') on the command line.
  94. cmdline = ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a', '',
  95.            '--beta', 'arg1', 'arg2']
  96.  
  97. if verbose:
  98.     print 'Running tests on getopt.getopt'
  99. opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta'])
  100. verify(opts == [('-a', '1'), ('-b', ''), ('--alpha', '2'), ('--beta', ''),
  101.                 ('-a', '3'), ('-a', ''), ('--beta', '')] )
  102. # Note ambiguity of ('-b', '') and ('-a', '') above. This must be
  103. # accounted for in the code that calls getopt().
  104. verify(args == ['arg1', 'arg2'])
  105.  
  106. expectException(
  107.     "opts, args = getopt.getopt(cmdline, 'a:b', ['alpha', 'beta'])",
  108.     GetoptError)
  109.  
  110. # Test handling of GNU style scanning mode.
  111. if verbose:
  112.     print 'Running tests on getopt.gnu_getopt'
  113. cmdline = ['-a', 'arg1', '-b', '1', '--alpha', '--beta=2']
  114. # GNU style
  115. opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
  116. verify(opts == [('-a', ''), ('-b', '1'), ('--alpha', ''), ('--beta', '2')])
  117. verify(args == ['arg1'])
  118. # Posix style via +
  119. opts, args = getopt.gnu_getopt(cmdline, '+ab:', ['alpha', 'beta='])
  120. verify(opts == [('-a', '')])
  121. verify(args == ['arg1', '-b', '1', '--alpha', '--beta=2'])
  122. # Posix style via POSIXLY_CORRECT
  123. os.environ["POSIXLY_CORRECT"] = "1"
  124. opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
  125. verify(opts == [('-a', '')])
  126. verify(args == ['arg1', '-b', '1', '--alpha', '--beta=2'])
  127.  
  128. #------------------------------------------------------------------------------
  129.  
  130. libreftest = """
  131. Examples from the Library Reference:  Doc/lib/libgetopt.tex
  132.  
  133. An example using only Unix style options:
  134.  
  135.  
  136. >>> import getopt
  137. >>> args = '-a -b -cfoo -d bar a1 a2'.split()
  138. >>> args
  139. ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
  140. >>> optlist, args = getopt.getopt(args, 'abc:d:')
  141. >>> optlist
  142. [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
  143. >>> args
  144. ['a1', 'a2']
  145.  
  146. Using long option names is equally easy:
  147.  
  148.  
  149. >>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
  150. >>> args = s.split()
  151. >>> args
  152. ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
  153. >>> optlist, args = getopt.getopt(args, 'x', [
  154. ...     'condition=', 'output-file=', 'testing'])
  155. >>> optlist
  156. [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]
  157. >>> args
  158. ['a1', 'a2']
  159.  
  160. """
  161.  
  162. __test__ = {'libreftest' : libreftest}
  163.  
  164. import sys
  165. run_doctest(sys.modules[__name__], verbose)
  166.  
  167. #------------------------------------------------------------------------------
  168.  
  169. if verbose:
  170.     print "Module getopt: tests completed successfully."
  171.